package router

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"time"
	. "website/handle"
	"website/utils"
)

func Run()  {
	r := gin.Default()
	r.Use(cors.New(cors.Config{
		AllowOrigins: []string{"*"},
		AllowMethods: []string{"PUT", "PATCH", "GET", "DELETE", "POST", "OPTIONS", "VIEW", "HEAD"},
		AllowHeaders: []string{
			"Origin", "Access-Control-Allow-Origin",
			"Content-Type", "Content-Length", "Accept-Encoding",
			"X-CSRF-Token", "Authorization", "accept", "origin",
			"Cache-Control", "X-Requested-With", "XMLHttpRequest",
			"X_FILENAME", "accept-encoding", "authorization",
			"content-type", "dnt", "origin", "user-agent",
			"x-csrftoken", "x-requested-with",
		},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			return origin == "*"
		},
		MaxAge: 24 * time.Hour,
	}))
	v1 := r.Group("/api/v1")
	{
		v1.POST("/contact", CreateContactInfo)
		v1.GET("/contact", FetchAllContactInfo)
		v1.GET("/contact/:id", FetchSingleContactInfo)
		v1.POST("/managers", CreateManagers)
		v1.GET("/managers", FetchAllManagers)
		v1.PUT("/managers/:id", UpdateManagers)
	}
	rcf := utils.ServerConfInit()
	if err := r.Run(rcf.Addr); err != nil {
		panic(err)
	}
}
